<#import "../common/layout.ftl" as layout/>
<#import "../common/dataTables.ftl" as dataTables/>
<#import "../common/modern-forms.ftl" as mform/>
<#import "../common/modal.ftl" as modal/>

<!DOCTYPE html>
<html>
<@layout.head title="接口日志">
    <@dataTables.head/>
    <@dataTables.buttons_head/>
    <@dataTables.select_head/>
    <@dataTables.fixedHeader_head/>
    <@mform.head/>
    <@select.head/>
</@layout.head>
<body>
<@layout.page>
    <div class="main-box">

        <!-- 表格 -->
        <div class="content">
            <!-- 按钮 -->
            <div id="mUserBtnGroup" class="btn-group">
                <input id="startdate" class="inp" name="startdate" placeholder='请选择开始日期' style="margin-left: 10px"/>
                <span>至</span>
                <input id="enddate" class="inp" name="enddate" placeholder='请选择结束日期'/>
                <button type="button" class="btn btn-blue btn-sm"
                        onclick="javascript:refreshData();">
                    <i class="fa fa-refresh"></i> 刷新
                </button>
                <button id="btnAddTopMenu" type="button" class="btn btn-blue btn-sm" style="margin-left: 5px;"
                        onclick="javascript:newServer();">
                    <i class="fa fa-plus"></i> 新增
                </button>
                <button id="btnAddTopMenu" type="button" class="btn btn-blue btn-sm" style="margin-left: 5px;"
                        onclick="javascript:log();">
                    <i class="fa fa-edit"></i> 日志详情
                </button>
                <button id="btnAddTopMenu" type="button" class="btn btn-blue btn-sm" style="margin-left: 5px;"
                        onclick="javascript:delServer();">
                    <i class="fa fa-minus"></i> 删除
                </button>


            </div>
            <!-- 表格 -->
            <@dataTables.commonTable id="mainTable"/>
        </div>
    </div>
    <!-- 弹出窗 -->
    <@modal.editModal id="modal-mUser" buttonId="btnmUserSave" width="500px">
        <@mform.container id="userEditForm">
            <input id="id" name="id" type="hidden"/>
            <@mform.row>
                <@mform.input_text id="logLevel" name="logLevel" size=6
                label="日志级别" placeholder="请输入" icon="fa-edit" />
                <@mform.input_text id="interfaceName" name="interfaceName" size=6
                label="接口名称" placeholder="请输入" icon="fa-edit" />
                <@mform.input_text id="interfacePattern" name="interfacePattern" size=6
                label="接口模式" placeholder="请输入" icon="fa-edit" />
                <@mform.input_text id="visitAddress" name="visitAddress" size=6
                label="访问地址" placeholder="请输入" icon="fa-edit" />
                <@mform.input_text id="sendContent" name="sendContent" size=6
                label="发送内容" placeholder="请输入" icon="fa-edit" />
                <@mform.input_text id="returnContent" name="returnContent" size=6
                label="返回内容" placeholder="请输入" icon="fa-edit" />
                <@mform.input_text id="keyWord1" name="keyWord1" size=6
                label="关键词1" placeholder="请输入" icon="fa-edit" />
                <@mform.input_text id="keyWord2" name="keyWord2" size=6
                label="关键词2" placeholder="请输入" icon="fa-edit" />
                <@mform.input_text id="keyWord3" name="keyWord3" size=6
                label="关键词3" placeholder="请输入" icon="fa-edit" />
                <@mform.input_text id="keyWord4" name="keyWord4" size=6
                label="关键词4" placeholder="请输入" icon="fa-edit" />
                <@mform.input_text id="keyWord5" name="keyWord5" size=6
                label="关键词5" placeholder="请输入" icon="fa-edit" />
                <@mform.input_text id="keyWord6" name="keyWord6" size=6
                label="关键词6" placeholder="请输入" icon="fa-edit" />
                <@mform.input_text id="keyWord7" name="keyWord7" size=6
                label="关键词7" placeholder="请输入" icon="fa-edit" />
                <@mform.input_text id="keyWord8" name="keyWord8" size=6
                label="关键词8" placeholder="请输入" icon="fa-edit" />
                <@mform.input_text id="returnContent" name="returnContent" size=6
                label="异常信息" placeholder="exceptionMessage" icon="fa-edit" />
                <@mform.select2 id='state' name='state' label="日志状态" size=6 >
                    <#list state as item>
                        <option value="${item.value}">${item.text}</option>
                    </#list>
                </@mform.select2>
            </@mform.row>

        </@mform.container>
    </@modal.editModal>

</@layout.page>
<@layout.foot>
    <@dataTables.foot/>
    <@dataTables.buttons_foot/>
    <@dataTables.select_foot/>
    <@dataTables.fixedHeader_foot/>
    <@mform.foot/>
    <script src="${request.contextPath}/assets/js/jquery.serializejson.min.js?v=3653060703"></script>
    <script type="text/javascript">
        var stateMap = {
            <#list state as item>
            "${item.value}": "${item.name}",
            </#list>
        }
        var now = new Date();
        var begin = now.getFullYear() + "-" + (now.getMonth() + 1) + "-1";
        var endD;
        var nextMonth = now.getMonth() + 1;
        var nextMonthFirstDay = new Date(now.getFullYear(), nextMonth, 1);
        var oneDay = 1000 * 60 * 60 * 24;
        endD = new Date(nextMonthFirstDay - oneDay);
        var end = endD.getFullYear() + "-" + (endD.getMonth() + 1) + "-" + endD.getDate();

        $("#startdate").datetimepicker({
            language: "zh-CN",
            startView: 2,
            minView: 2,
            format: "yyyy-mm-dd",
            autoclose: true,
            initialDate: new Date(),
            todayBtn: true
        });
        $("#startdate").val(begin);
        $("#enddate").datetimepicker({
            language: "zh-CN",
            startView: 2,
            minView: 2,
            format: "yyyy-mm-dd",
            autoclose: true,
            todayBtn: true
        });
        $("#enddate").val(end);
        var columns =
            [
                {"sWidth": "40px", "title": "日志级别", "data": "logLevel"},
                {"sWidth": "40px", "title": "接口名称", "data": "interfaceName"},
                {"sWidth": "40px", "title": "接口模式", "data": "interfacePattern"},
                {"sWidth": "40px", "title": "访问地址", "data": "visitAddress"},
                {"sWidth": "200px", "title": "发送内容", "data": "sendContent"},
                {"sWidth": "200px", "title": "返回内容", "data": "returnContent"},
                {"sWidth": "40px", "title": "关键词1", "data": "keyWord1"},
                {"sWidth": "40px", "title": "关键词2", "data": "keyWord2"},
                {"sWidth": "40px", "title": "关键词3", "data": "keyWord3"},
                {"sWidth": "40px", "title": "关键词4", "data": "keyWord1"},
                {"sWidth": "40px", "title": "关键词5", "data": "keyWord2"},
                {"sWidth": "40px", "title": "关键词6", "data": "keyWord3"},
                {"sWidth": "40px", "title": "关键词7", "data": "keyWord1"},
                {"sWidth": "40px", "title": "关键词8", "data": "keyWord2"},
                {"sWidth": "40px", "title": "关键词9", "data": "keyWord3"},
                {"sWidth": "40px", "title": "关键词10", "data": "keyWord1"},
                {"sWidth": "100px", "title": "异常信息", "data": "exceptionMessage"},
                {"sWidth": "120px", "title": "新增时间", "data": "createTime"},
                {"sWidth": "40px", "title": "修改时间", "data": "updateTime"},
                {"sWidth": "40px", "title": "日志状态", "data": "state"}
            ];
        var mainTable = $('#mainTable').winningTable({
            columns: columns,
            pageLength: 10,
            toolbar: "#mUserBtnGroup",
            /*select: {style: 'multi'},*/
            fixedHeader: false,
            searching: true,
            ordering: true,
            "columnDefs": [{
                "targets": [0,1,2,3,6,7,8,9,10,11,12,13,14,15,18,19],
                "visible": false
            }],
            buttons: [

            ],
            "rowCallback": function (row, data, index) {
                $('td', row).eq(14).html( stateMap[data.state] );
            }
        });
        function newServer() {
            $('#modal-mUser').find(".modal-title").text('新增日志信息');
            clearForm($('#modal-mUser'));
            $("#btnmUserSave").unbind();
            $("#btnmUserSave").click(saveServerNew);
            $('#modal-mUser').modal();
        }
        function editServer() {
            var datas = mainTable.getSelectedDatas();
            if (datas == null || datas.length == 0 || datas.length > 1) {
                swal("错误", "请选择一行数据", "error");
                return;
            }
            var $modal = $("#modal-mUser");
            $modal.find(".modal-title").text("修改日志信息");
            clearForm($modal);
            var data = datas[0];
            initFormItemData($("#userEditForm"), data);
            $modal.modal();
            $("#btnmUserSave").unbind();
            $("#btnmUserSave").click(saveServerNew);

        }
        function refreshData() {
            var startdate = $("#startdate").val();
            var enddate = $("#enddate").val();
            var params = {
                datas: JSON.stringify({
                    b_time: startdate,
                    e_time: enddate,
                })
            };
            mainTable.queryData("${request.contextPath}/interfaceLog/queryAll/"+${id});
        }
        refreshData()
        function delServer() {
            var datas = mainTable.getSelectedDatas();
            if (datas == null || datas.length == 0 || datas.length > 1) {
                swal("错误", "请选择一行数据", "error");
                return;
            }
            var data = datas[0];
            swal({
                title: "确定删除",
                text: "是否确定删除此数据",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "是的,确定",
                cancelButtonText: "取消"
            }).then(function (result) {
                if(!result.value)
                    return;
                var requestData = {
                    id:data.id
                };
                var params = {datas: JSON.stringify(requestData)};
                $.ajax({
                    type: 'POST',
                    data: params,
                    url: '${request.contextPath}/interfaceLog/delete',
                    success: function (data) {
                        //成功之后
                        if (data.success) {
                            swal({
                                title: "成功",
                                text: "操作成功!",
                                type: "success",
                                showCancelButton: false,
                                confirmButtonText: "确定"
                            }).then(function (result) {
                                refreshData();
                            });
                        }
                        else {
                            swal("错误", data.errorMsg, "error");
                        }
                    },
                    error: function (xhr, type) {
                        swal("错误", "系统发生内部错误!请稍后再试!", "error");
                    }
                });
            });
        }

        function saveServerNew() {

            swal({
                title: "确定新增",
                text: "是否确定新增此数据",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "是的,确定",
                cancelButtonText: "取消"
            }).then(function (result) {
                if (!result.value)
                    return;
                var requestData = $("#modal-mUser form").serializeJSON();
                var params = {datas: JSON.stringify(requestData)};
                $.ajax({
                    type: 'POST',
                    data: params,
                    url: '${request.contextPath}/interfaceLog/save',
                    success: function (data) {
                        //成功之后
                        if (data.success) {
                            swal({
                                title: "成功",
                                text: "操作成功!",
                                type: "success",
                                showCancelButton: false,
                                confirmButtonText: "确定"
                            }).then(function (result) {
                                $("#modal-mUser").modal('hide');
                                refreshData();
                            });
                        } else {
                            swal("错误", data.errorMsg, "error");
                        }
                    },
                    error: function (xhr, type) {
                        swal("错误", "系统发生内部错误!请稍后再试!", "error");
                    }
                });
            });
        }
        function log() {
            var datas = mainTable.getSelectedDatas();

            if (datas == null || datas.length == 0 || datas.length > 1) {
                swal("错误", "请选择一行数据", "error");
                return;
            }
            var data = datas[0];
            top.addTabs({
                id: "log"+data.id,
                title: "日志详细信息",
                close: 'ture',
                url: "${request.contextPath}/interfaceLog/detailMessage/" + data.id
            })
        }
    </script>
</@layout.foot>
</body>
</html>